﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KenLib.BusinessManager;
using System.Collections.Generic;
using DataInfo;
using System.Reflection;

public partial class frm_TraineeContactInfo : System.Web.UI.Page
{
    string strErr = "";
    string strControlsNotRequireValue = "_住所2建物名 ,_住所１建物名 ";
    string strControlSetValue = "住民票住所,住所１,住所１建物名,住所２,住所２建物名,居住開始日";
    int id = -1;
    public int iSessionLogin = 0;
    GlobalManager maGlobal = new GlobalManager();
    家族情報Manager ma家族情報 = new 家族情報Manager();
    本人住所情報Manager ma本人住所情報 = new 本人住所情報Manager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["SessionLoginID"] == null)
        {
            Response.Redirect("frm_Menu.aspx");
        }
        if (Request.QueryString["id"] != null)
            id = Convert.ToInt32(Request.QueryString["id"]);
        else
            Response.Redirect(AppCode_Common.getHistory(Page));
        iSessionLogin = (int)Session["Login"];
       // lbl_NameHeader.Text = AppCode_Common.GetUserName(Session["SessionLoginID"].ToString());
        if (!IsPostBack)
        {
            ViewState["ID"] = -1;
            for (int i = DateTime.Now.Year; i >= 1900; i--)
                ddl_居住開始日y.Items.Add(new ListItem(i.ToString()));
            List<本人住所情報 > ls本人住所情報 = maGlobal.GetInfoInTableByValue<本人住所情報>("本人住所情報", "社員ID", id.ToString(), false);
            if (ls本人住所情報.Count > 0)
            {
                ViewState["ID"] = ls本人住所情報[0].ID;
                SetValueIntoControls(ls本人住所情報[0]);
                btn_登録.Text = "Chỉnh sửa";
            }
        }
        ((MasterPage)this.Master).changeTitle("THÔNG TIN LIÊN LẠC");
    }
    void SetValueIntoControls(本人住所情報 info本人住所情報)
    {
        string[] ControlNames = strControlSetValue.Split(',');
        Type type = info本人住所情報.GetType();
        foreach (string strName in ControlNames)
        {
            TextBox txt = (TextBox)div_Body.FindControl("txt_" + strName);
            if (txt != null)
            {
                PropertyInfo infoProperty = type.GetProperty(strName);
                object txtValue = infoProperty.GetValue(info本人住所情報, null);
                if (txtValue != null)
                    txt.Text = txtValue.ToString();
            }
        }
        ddl_居住開始日y.SelectedValue = Convert.ToDateTime(info本人住所情報.居住開始日).Year.ToString();
        ddl_居住開始日d.SelectedValue = Convert.ToDateTime(info本人住所情報.居住開始日).Day.ToString();
        ddl_居住開始日m.SelectedValue = Convert.ToDateTime(info本人住所情報.居住開始日).Month.ToString();
    }
    本人住所情報 GetInfoFromControls()
    {
        string[] ControlNames = strControlSetValue.Split(',');
        本人住所情報 info本人住所情報 = new 本人住所情報();
        Type type = info本人住所情報.GetType();
        foreach (string strName in ControlNames)
        {
            PropertyInfo infoProperty = type.GetProperty(strName);
            Control c = div_Body.FindControl("txt_" + infoProperty.Name);
            if (c != null)
                infoProperty.SetValue(info本人住所情報, AppCode_Common.getValue(infoProperty, (TextBox)c), null);
        }
        info本人住所情報.居住開始日 = new DateTime(Convert.ToInt32(ddl_居住開始日y.SelectedValue), Convert.ToInt32(ddl_居住開始日m.SelectedValue), Convert.ToInt32(ddl_居住開始日d.SelectedValue));
        info本人住所情報.社員ID = id;
        info本人住所情報.ID = Convert.ToInt32(ViewState["ID"]);
        return info本人住所情報;
    }
    bool Validation()
    {
        bool bReturn = true;
        string strControlRequireValueName = "txt_住民票住所,txt_住所１,ddl_居住開始日d,ddl_居住開始日m,ddl_居住開始日y,txt_住所２";
        string strControlRequireValueText = "Nơi đăng ký thường trú ,Địa chỉ,Thời gian bắt đầu cư trú ,Thời gian bắt đầu cư trú,Thời gian bắt đầu cư trú,Địa chỉ";
        string[] arrStrControlRequireValueName = strControlRequireValueName.Split(',');
        string[] arrStrControlRequireValueText = strControlRequireValueText.Split(',');
        for (int i = 0; i < arrStrControlRequireValueName.Length && bReturn; i++)
        {
            Control c = div_Body.FindControl(arrStrControlRequireValueName[i]);
            if (c != null)
            {
                if (c.GetType().Name == "TextBox" && ((TextBox)c).Text == "")
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "show", "alert('Hãy nhập thông tin " + arrStrControlRequireValueText[i] + "');", true);
                    c.Focus();
                    bReturn = false;
                }
                else if (c.GetType().Name == "DropDownList" && ((DropDownList)c).SelectedValue == "-1")
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "show", "alert('Hãy nhập thông tin " + arrStrControlRequireValueText[i] + "');", true);
                    c.Focus();
                    bReturn = false;
                }
            }
        }
        if (bReturn)
        {
            if (DateTime.DaysInMonth(Convert.ToInt32(ddl_居住開始日y.SelectedValue), Convert.ToInt32(ddl_居住開始日m.SelectedValue)) < Convert.ToInt32(ddl_居住開始日d.SelectedValue))
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "show", "alert('Ngày không hợp lệ');", true);
                ddl_居住開始日d.Focus();
                bReturn = false;
            }
        }
        return bReturn;
    }
    
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        if (Validation())
        {
            string strColumnsName = "廃止区分,住民票住所,郵便番号１,住所１,住所１建物名,郵便番号2,住所２,住所２建物名,電話番号,携帯番号,EMail,住居区分,居住開始日,居住終了日,契約家主," +
                                    "斡旋会社,家賃,敷金,礼金,住宅手当,支給開始日,支給終了日,宿泊確保機関,宿泊確保機関内容,社員ID";
            本人住所情報 info本人住所情報 = GetInfoFromControls();
            int exe = ma本人住所情報.Ins_Upd本人住所情報(info本人住所情報, strColumnsName);
            if (exe != 0 && Session["TraineeFamilyInfo"] != null)
            {
                strColumnsName = "廃止区分,続柄コード,続柄,登録年月日,修正年月日,氏名,フリガナ,性別,生年月日,年齢,国籍,同居区分,死亡年月日,備考,同伴区分,郵便番号,住所１" +
                                            ",住所２,住居国情報,電話番号,EMail,勤務先･通学先,職業名,外国人登録番号,家族年収,家族手当発生日,家族手当申請理由,家族手当,配偶者控除区分" +
                                            ",配偶者控除発生日,配偶者控除申請理由,扶養控除区分,扶養控除発生日,扶養控除申請理由,障害控除区分,社員ID";
                List<家族情報> ls家族情報 = (List<家族情報>)Session["TraineeFamilyInfo"];
                foreach (家族情報 info家族情報 in ls家族情報)
                {
                    ma家族情報.Ins_Upd家族情報(info家族情報, strColumnsName);
                }
            }
            Response.Redirect("frm_Menu.aspx?page=TraineeContactInfo");
        }
    }
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        foreach (Control c in div_Body.Controls)
        {
            if (c.GetType().Name == "TextBox")
            {
                ((TextBox)c).Text = "";
            }
            else if (c.GetType().Name == "DropDownList")
            {
                ((DropDownList)c).SelectedIndex = 0;
            }
            else if (c.GetType().Name == "CheckBox")
            {
                ((CheckBox)c).Checked = false;
            }
            else if (c.GetType().Name == "RadioButton")
            {
                ((RadioButton)c).Checked = false;
            }
        }
    }
}
